package gui.utils;

import gui.processor.Processor;
import gui.processor.Worker;
import gui.Segl0;
import gui.North;
import gui.win.data.rm.RmD;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import rpgsdb.db.NK;
import java.awt.Dimension;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class ComboBox extends JPanel implements ActionListener{
    public static int PROJECT_SELECT_AND_LINKED_DATASET_TO_REMOVE_SELECT = 2;
    public static int PROJECT_SELECT_ADDING_REMOVING = 3;
    public static int REMOVE_DATASET_SELECT = 4;
    public static int BROWSER_DATASET_SELECT = 5;
    public static int TREE_SECONDARY_DATASET_SELECT = 7;
    public static int PROJECT_SELECT_AND_LINKED_DATASET_MAIN_BROWSER = 8;
    public static int PPROJECT_FOR_BAYSEAN_PRIORS = 9;

    private JComboBox mods;

    private int width, height, which;

    public ComboBox(String title, int which, int width, int height) {
        super();
        this.which = which;
        this.width = width;
        this.height = height;
        this.setBorder(BorderFactory.createEmptyBorder(8,3,8,3));
        this.setLayout(new BorderLayout());
        if(title!=null){
            JLabel modelLabel = new JLabel(title);
            modelLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,6));
            modelLabel.setFont(Borders.getFont(Borders.GUIFONT));
            this.add(modelLabel, BorderLayout.WEST);
        }
        this.add(mods = fillBox(), BorderLayout.EAST);
    }

    private RmD RmDataL0;
    public ComboBox(String title, int which,  RmD RmDataL0, int width, int height) {
        super();
        this.which = which;
        this.width = width;
        this.height = height;
        this.RmDataL0 = RmDataL0;
        this.setBorder(BorderFactory.createEmptyBorder(8,3,8,3));
        this.setLayout(new BorderLayout());
        if(title!=null){
            JLabel modelLabel = new JLabel(title);
            modelLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,6));
            modelLabel.setFont(Borders.getFont(Borders.GUIFONT));
            this.add(modelLabel, BorderLayout.CENTER);
        }
        this.add(mods = fillBox(), BorderLayout.EAST);
    }

    private North north;
    public ComboBox(String title, int which, North L2_North, int width, int height) {
        super();
        this.which = which;
        this.north = L2_North;
        this.width = width;
        this.height = height;
        this.setBorder(BorderFactory.createEmptyBorder(8,-2,6,0));
        this.setLayout(new BorderLayout());
        if(title!=null){
            JLabel modelLabel = new JLabel(title);
            modelLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,6));
            modelLabel.setFont(Borders.getFont(Borders.GUIFONT));
            this.add(modelLabel, BorderLayout.CENTER);
        }
        this.add(mods = fillBox(), BorderLayout.EAST);
    }

    public int [] correspondingDBKeys;
    private JComboBox fillBox(){
        String[] models = null;
        if(which==PROJECT_SELECT_ADDING_REMOVING || which == PROJECT_SELECT_AND_LINKED_DATASET_TO_REMOVE_SELECT){
            ArrayList <NK> namesAndKeys = Segl0.db.getProjNK();
            namesAndKeys.add(0, new NK("Select Project", -1));
            models = new String[namesAndKeys.size()];
            correspondingDBKeys = new int[namesAndKeys.size()];
            for(int x=0; x<models.length; x++){
                models[x] = namesAndKeys.get(x).getN();
                correspondingDBKeys[x] = namesAndKeys.get(x).getK();
            }
            mods = new JComboBox(models);
            mods.setPreferredSize(new Dimension(width, height));
            mods.setFont(Borders.getFont(Borders.GUIFONT));
            mods.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            mods.setSelectedItem(models[0]);
            Para.P_ID_ADD_RM = correspondingDBKeys[0];
        }
        else if(which == PROJECT_SELECT_AND_LINKED_DATASET_MAIN_BROWSER){
            ArrayList <NK> namesAndKeys = Segl0.db.getProjNK();
            namesAndKeys.add(0, new NK("Select Project", -1));
            models = new String[namesAndKeys.size()];
            correspondingDBKeys = new int[namesAndKeys.size()];
            for(int x=0; x<models.length; x++){
                models[x] = namesAndKeys.get(x).getN();
                correspondingDBKeys[x] = namesAndKeys.get(x).getK();
            }
            mods = new JComboBox(models);
            mods.setPreferredSize(new Dimension(width, height));
            mods.setFont(Borders.getFont(Borders.GUIFONT));
            mods.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            
            if(Para.D_ID!=-1){
                //get index of project
                int index = 0;
                for(int x=0; x<models.length; x++){
                    if(Para.D_ID==namesAndKeys.get(x).getK())
                    index = x;
                    x = models.length;
                }
                mods.setSelectedItem(models[index]);
            }
            else{
                Para.PID = correspondingDBKeys[0];
                mods.setSelectedItem(models[0]);
            }
        }
        else if(which==REMOVE_DATASET_SELECT){
            ArrayList <NK> namesAndKeys = Segl0.db.getDataNK(Para.P_ID_ADD_RM);
            namesAndKeys.add(0, new NK("Select Dataset", -1));
            models = new String[namesAndKeys.size()];
            correspondingDBKeys = new int[namesAndKeys.size()];
            for(int x=0; x<models.length; x++){
                models[x] = namesAndKeys.get(x).getN();
                correspondingDBKeys[x] = namesAndKeys.get(x).getK();
            }
            mods = new JComboBox(models);
            mods.setPreferredSize(new Dimension(width, height));
            mods.setFont(Borders.getFont(Borders.GUIFONT));
            mods.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            mods.setSelectedItem(models[0]);
            Para.D_ID_ADD_RM = correspondingDBKeys[0];
        }
        else if(which == BROWSER_DATASET_SELECT){
            ArrayList <NK> namesAndKeys = Segl0.db.getDataNK(Para.PID);
            namesAndKeys.add(0, new NK("Select Dataset", -1));
            models = new String[namesAndKeys.size()];
            correspondingDBKeys = new int[namesAndKeys.size()];
            for(int x=0; x<models.length; x++){
                models[x] = namesAndKeys.get(x).getN();
                correspondingDBKeys[x] = namesAndKeys.get(x).getK();
            }
            mods = new JComboBox(models);
            mods.setPreferredSize(new Dimension(width, height));
            mods.setFont(Borders.getFont(Borders.GUIFONT));
            mods.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            mods.setSelectedItem(models[0]);
            Para.D_ID = correspondingDBKeys[0];
        }
        else if(which == TREE_SECONDARY_DATASET_SELECT){
            ArrayList <NK> namesAndKeys = Segl0.db.getDataNK(Para.PID);
            namesAndKeys.add(0, new NK("Additional dataset  (optional)", -1));
            for(int x=namesAndKeys.size()-1; x>=0; x--){
                if(namesAndKeys.get(x).getK() == Para.D_ID){
                    namesAndKeys.remove(x);x=-1;
                }
            }
            namesAndKeys.add(namesAndKeys.size(), new NK("All  (slow)", -2));
            models = new String[namesAndKeys.size()];
            correspondingDBKeys = new int[namesAndKeys.size()];

            for(int x=0; x<models.length; x++){
                models[x] = namesAndKeys.get(x).getN();
                correspondingDBKeys[x] = namesAndKeys.get(x).getK();
            }

            mods = new JComboBox(models);
            mods.setSelectedItem(0);
        }

        mods.addActionListener(this);
        mods.setPreferredSize(new Dimension(width, height));
        mods.setFont(Borders.getFont(Borders.GUIFONT));
        mods.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        mods.setFont(Borders.getFont(Borders.GUIFONT));

        return mods;
    }

    public void actionPerformed(ActionEvent evt){
        if(which==PROJECT_SELECT_ADDING_REMOVING ){
            Para.P_ID_ADD_RM = correspondingDBKeys[mods.getSelectedIndex()];
        }
        else if(which == PROJECT_SELECT_AND_LINKED_DATASET_TO_REMOVE_SELECT){
            Para.P_ID_ADD_RM = correspondingDBKeys[mods.getSelectedIndex()];
            RmDataL0.resetAssociatedDatasetCombobox();
        }
        else if(which == PROJECT_SELECT_AND_LINKED_DATASET_MAIN_BROWSER){
            Para.PID = correspondingDBKeys[mods.getSelectedIndex()];
            north.resetAssociatedDatasetCombobox();
        }
        else if(which == BROWSER_DATASET_SELECT){
            Para.D_ID = correspondingDBKeys[mods.getSelectedIndex()];
            Processor gp = new Processor(north);
            gp.setTask(Para.TASK_DISPLAY);
            Worker sl = new Worker(gp);
            sl.execute();
        }
        else if(which == REMOVE_DATASET_SELECT){
            Para.D_ID_ADD_RM = correspondingDBKeys[mods.getSelectedIndex()];
        }
        else if(which == TREE_SECONDARY_DATASET_SELECT){
            if(correspondingDBKeys[mods.getSelectedIndex()]==-2){
                Para.TREEDIDS = new int[correspondingDBKeys.length];
                Para.TREEDIDS[0] = Para.D_ID;
                int counter = 1;
                for(int x=0; x<correspondingDBKeys.length; x++){
                    if(correspondingDBKeys[x]!=-2){
                        Para.TREEDIDS[counter++] = correspondingDBKeys[x];
                    }
                }
            }
            else{
                Para.TREEDIDS = new int[2];
                Para.TREEDIDS[0] = Para.D_ID;
                Para.TREEDIDS[1] = correspondingDBKeys[mods.getSelectedIndex()];
            }
        }
    }

}